import UniRouteGuards from 'uniapp-route-guards'
import { useUserStore } from '@/store/user'

// 登录拦截白名单，在此白名单中的页面不做拦截
const whiteList = [
	'/package_user/pages/common/login',
	'/package_user/pages/common/rule'
]

const router = new UniRouteGuards()
router.beforeEach((to, from, next) => {
	if (to.action != 'navigateTo') {
		next()
		return
	}
  let url = to.url.toString()
	if (!url.startsWith('/')) {
		url = '/' + url
	}
	if (url.indexOf('?') != -1) {
		url = url.substring(0, url.indexOf('?'))
	}
	console.log('router intercept', url);
  const isInWhiteList = whiteList.findIndex(item => item == url) != -1
  if (!isInWhiteList && !useUserStore().isLogin) {
    uni.navigateTo({
      url: '/package_user/pages/common/login',
    })
    return
  }
  next()
})
